#include "Defs.h"
#include "Config.h"


Config_T::Config_T(const char * szDefaultLogFilesDir, const char * szConfigFile)
	:isBannerDone(false)
	,isHighPriorityRule("HighPriority", 1, "Setup", szConfigFile)
	,zonesRule("ZonesToExclude", "ZonesToInclude", "Setup", szConfigFile)
{
	Rules_T::Load();
	Log_T::Load();

	// 
	// Get RulesFile option
	//

	FillMemory(szRulesFile, sizeof(szRulesFile), 0);
	GetPrivateProfileString("Setup", "RulesFile", szConfigFile, 
		szRulesFile, sizeof(szRulesFile) - 1, szConfigFile);

	if( !String_T(szRulesFile).Equals(szConfigFile, false) )
	{
		char szData[MAX_PATH];
		FillMemory(szData, sizeof(szData), 0);
		RtlMoveMemory(szData, szConfigFile, String_T(szConfigFile).LastIndexOf("\\") + 1);
		lstrcat(szData, szRulesFile);

		FillMemory(szRulesFile, sizeof(szRulesFile), 0);
		_snprintf(szRulesFile, sizeof(szRulesFile) - 1, szData);
	}

	rulesPtr = Rules_T::Load(szDefaultLogFilesDir, szRulesFile);

	//
	// Dump the configuration options
	//

	char szBuffer[8192];
	FillMemory(szBuffer, sizeof(szBuffer), 0);

	unsigned int bufferSize = sizeof(szBuffer) - 1;
	unsigned int bufferCursor = 0;

	_snprintf(szBuffer + bufferCursor, bufferSize - 1 - bufferCursor, 
		"# ================ Setup\r\n");
	bufferCursor = lstrlen(szBuffer);

	// 
	// Dump HighPriority option
	//

	isHighPriorityRule.ToString(szBuffer + bufferCursor, bufferSize - 1 - bufferCursor);
	bufferCursor = lstrlen(szBuffer);

	// 
	// Dump RulesFile option
	//
	
	_snprintf(szBuffer + bufferCursor, bufferSize - 1 - bufferCursor, 
		"# RulesFile=%s\r\n", szRulesFile);
	bufferCursor = lstrlen(szBuffer);

	// 
	// Dump ZonesToExclude option
	//
	
	zonesRule.ToString(szBuffer + bufferCursor, bufferSize - 1 - bufferCursor);
	bufferCursor = lstrlen(szBuffer);

	// 
	// end
	//
	
	_snprintf(szBuffer + bufferCursor, bufferSize - 1 - bufferCursor, 
		"# ================ end\r\n");
	bufferCursor = lstrlen(szBuffer);

	//
	// write to trace
	//

	Trace_T::Initialize(szBuffer);

	// 
	// load each zone
	//

	for( unsigned int ix = 0 ; ix < zonesRule.tokenList.Count() ; ix++ )
	{
		if( Zone_T::IsValid(zonesRule.tokenList[ix]->ToString().Ptr(), szConfigFile, !zonesRule.flag) )
		{
			zoneList.Add(new Zone_T(
				zonesRule.tokenList[ix]->ToString().Ptr(),
				rulesPtr->szLogFilesDir, 
				szRulesFile, 
				szConfigFile, 
				!zonesRule.flag));
		}
		else
		{
			Trace_T::Write("Zone %s ignored because neither TargetAddr, TargetPort and TargetUrl are specified", zonesRule.tokenList[ix]->ToString().Ptr());
		}
	}

	// 
	// dump each zone
	//

	FillMemory(szBuffer, sizeof(szBuffer), 0);

	bufferSize = sizeof(szBuffer) - 1;
	bufferCursor = 0;

	for( unsigned int ix = 0 ; ix < zoneList.Count() ; ix++ )
	{
		zoneList[ix]->ToString(szBuffer + bufferCursor, bufferSize - 1 - bufferCursor);
		bufferCursor = lstrlen(szBuffer);
	}

	//
	// write to trace
	//

	Trace_T::Initialize(szBuffer);
}

Config_T::~Config_T()
{
	Rules_T::Unload();
	Log_T::Unload();
}
